<template>
  <div>
    <el-dialog :title="dialogtitle" width="1200px" class="icon-dialog" :visible.sync="show" :before-close="closeForm"
      append-to-body @open="open">
      <el-form ref="form" :size="size" :model="form" :rules="rules"
        :label-width="$store.getters.device !== 'mobile'?'16%':'90px'">
        <el-tabs v-model="activeName">
          <el-tab-pane style="padding-top:10px" label="基本信息" name="基本信息">
            <el-row>
              <el-col :span="24">
                <el-form-item label="卡类型">
                  <el-select v-model="form.card_tid" style="width:100%" filterable clearable placeholder="请选择">
                    <el-option v-for="(item,i) in card_tidarray" :key="i" :label="item.key" :value="item.val" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="名称" prop="name">
                  <el-input v-model="form.name" auto-complete="off" clearable placeholder="请输入名称" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="标签" prop="keyword">
                  <Tag :tag-list.sync="form.keyword" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="分类" prop="cat_id">
                  <Treeselect v-model="form.cat_id" :default-expand-level="1" :options="cidarray"
                    :normalizer="normalizer" :show-count="true" z-index="999999" placeholder="请选择所分类" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="图片" prop="images">
                  <ImagesUpload size="mini" file-type="images" :images.sync="form.images" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="赠送金额">
                  <el-input-number
                    v-model="form.extraprice"
                    controls-position="right"
                    style="width:200px;margin-right: 10px;"
                    auto-complete="off"
                    clearable
                    :min="2"
                    placeholder="赠送金额"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="状态" prop="status">
                  <el-switch v-model="form.status" :active-value="1" :inactive-value="0" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="排序">
                  <el-input-number v-model="form.sort" controls-position="right" style="width:200px;"
                    auto-complete="off" clearable :min="0" placeholder="排序" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane style="padding-top:10px" label="详情" name="详情">
            <el-row>
              <el-col :span="24">
                <el-form-item label="详情" prop="description">
                  <WangEditor v-if="show" :wang-editor-content.sync="form.description" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane style="padding-top:10px" label="视频" name="视频">
            <el-row>
              <el-col :span="24">
                <el-form-item label="视频上传">
                  <ImagesUpload size="mini" file-type="file" :file.sync="form.videourl" />
                  <div class="help-block">支持格式：mp4,3gp,m3u8</div>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="视频(网络地址)">
                  <el-input v-model="form.videourl" auto-complete="off" clearable placeholder="视频" />
                  <div class="help-block">如果视频已上传到服务，请复制连接填写到这里</div>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
          <el-tab-pane style="padding-top:10px" label="其他信息" name="其他信息">
            <el-row>
              <el-col :span="24">
                <el-form-item label="型号">
                  <el-input v-model="form.model" auto-complete="off" clearable placeholder="请输入型号" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="销量基数">
                  <el-input-number v-model="form.sale_count_base" controls-position="right" style="width:300px;"
                    auto-complete="off" clearable :min="0" placeholder="请输入销量基数" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="点击量基数">
                  <el-input-number v-model="form.viewed_base" controls-position="right" style="width:300px;"
                    auto-complete="off" clearable :min="0" placeholder="请输入点击量基数" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="产品所在地">
                  <el-input v-model="form.location" auto-complete="off" clearable placeholder="请输入产品所在地" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="联系电话">
                  <el-input v-model="form.tel" auto-complete="off" clearable placeholder="请输入联系电话" />
                  <div class="help-block">填写了电话后，客户电话咨询购买</div>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="最小起订数目">
                  <el-input-number v-model="form.minimum" controls-position="right" style="width:300px;"
                    auto-complete="off" clearable :min="0" placeholder="请输入最小起订数目" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="loading" type="primary" @click="submit">
          <span v-if="!loading">保 存</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="closeForm">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import Treeselect from '@riophae/vue-treeselect'
  import Tag from '@/components/common/Tag.vue'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import ImagesUpload from '@/components/common/ImagesUpload.vue'
  import WangEditor from '@/components/common/WangEditor.vue'
  export default {
    name: 'goodsgiftcardupdate',
    components: {
      Treeselect,
      WangEditor,
      ImagesUpload,
      Tag
    },
    props: {
      show: {
        type: Boolean,
        default: false
      },
      opentype: {
        type: String,
        default: 'add'
      },
      size: {
        type: String,
        default: 'small'
      },
      info: {
        type: Object
      }
    },
    data() {
      return {
        lang: {},
        dialogtitle: '',
        form: {
          title: '',
          timediscount: [{
            begin_time: "",
            end_time: "",
            addsubtract: "",
            price: ""
          }],
          sourceAttribute: [],
          attribute: [],
          sku: [],
          keyword: [],
          status: 1
        },
        card_tidarray: [],
        cidarray: [],
        unitarray:[],
        loading: false,
        rules: {
          title: [{
            required: true,
            message: '显示文字不能为空',
            trigger: 'blur'
          }]
        },
        theme: 2,
        structure: [{
          name: 'image',
          type: 'slot',
          label: '图片'
        }, {
          name: 'price',
          type: 'input',
          label: '价格'
        }],
        activeName: '基本信息'
      }
    },
    mounted() {
      this.$api.post('/Lang/getlang').then(res => {
        this.lang = res.data
      })
    },
    watch: {
      show(val) {
        if (val) {
          this.$api.post('/goods/getField', {
            ptype: 2
          }).then(res => {
            this.cidarray = res.data.cidarray
            this.card_tidarray = res.data.card_tidarray
            this.unitarray = res.data.unitarray
          })
        }
      }
    },
    methods: {
      open() {
        this.activeName = '基本信息'
        if (this.opentype == 'update') {
          this.dialogtitle = '修改'
        } else {
          this.dialogtitle = '添加'
        }
        this.form = this.info
      },
      submit() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.loading = true
            this.form.is_times = 3
            this.$api.post('/goods/update', this.form).then(res => {
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.$emit('refesh_list')
              this.closeForm()
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      closeForm() {
        this.$emit('update:show', false)
        this.loading = false
        if (this.$refs['form'] !== undefined) {
          this.$refs['form'].resetFields()
        }
      },
      normalizer(data) {
        if (data.children && !data.children.length) {
          delete data.children
        }
        return {
          id: data.val,
          label: data.key,
          children: data.children
        }
      },
      addItem() {
        let that = this;
        that.form.timediscount.push({
          begin_time: "",
          end_time: "",
          addsubtract: "",
          price: ""
        });
      },
      deleteItem(i) {
        if (this.form.timediscount.length <= 1) {
          return false;
        }
        this.form.timediscount.splice(i, 1);
      }
    }
  }
</script>
